﻿using Newtonsoft.Json;
using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Currency.FileUtils;
using SpiderStudy.Utils.Helper;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite.Bcy
{
    internal class BcyComment
    {
        /// <summary>
        /// 文件夹路径
        /// </summary>
        private static  string FileFloder = StaticValue.TempPath + "Bcy\\";

        /// <summary>
        /// 评论名单列表
        /// </summary>
        private string ExcludePath =FileFloder + "BcyExclude.txt";

        /// <summary>
        /// 评论列表
        /// </summary>
        private string CommentPath = FileFloder + "BcyComment.txt";

        public void ScannerListFile(string FileName)
        {
            List<string> UrlList = FileHelper.ReadAllLine(FileName);

            foreach (string SingleLink in UrlList)
            {
                String LinkID = new FileNameHelper().GetFileNameWithoutExtension(SingleLink);
                Console.WriteLine(LinkID);
                FileHelper.AppendUTF8List(CommentPath, GetComment(LinkID));
            }
        }
        /// <summary>
        /// 清理不合规定的关键词
        /// </summary>
        /// <param name="FilePath"></param>
        public void CommentClear(string FilePath)
        {
            List<string> ExcludeList = FileHelper.ReadAllLine(ExcludePath);
            List<string> Comment = FileHelper.ReadAllLine(FilePath);
        //  Comment = ListHelper.ListRepeat(Comment);
            Comment = new ListHelper().ListRemoveExclude(Comment, ExcludeList);
            FileHelper.WriteUTF8List(new FileNameHelper().CreateFileNameAddTip(FilePath, "_清理"), Comment);

        }

        /// <summary>
        /// 获取链接的评论
        /// </summary>
        /// <param name="linkID"></param>
        /// <returns></returns>
        private List<string> GetComment(string LinkID)
        {
            String API = string.Format("https://bcy.net/item/Reply/replyloadmore?page=1&item_id={0}", LinkID);
            List<string> TempList = new List<string>();
            string Shtml = WebHelper.HttpGet(API);
            try
            {
                Root JsonRoot = JsonConvert.DeserializeObject<Root>(Shtml);
                foreach (DataItem SingleComment in JsonRoot.data)
                {
                    TempList.Add(SingleComment.content);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return TempList;
        }

        public class CommentsItem
        {
            /// <summary>
            ///
            /// </summary>
            public string id { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string uid { get; set; }

            /// <summary>
            /// 谢谢！mua~
            /// </summary>
            public string content { get; set; }

            /// <summary>
            ///
            /// </summary>
            public int ctime { get; set; }

            /// <summary>
            ///
            /// </summary>
            public int like_count { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string user_liked { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string uname { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string avatar { get; set; }
        }

        public class UtagsItem
        {
            /// <summary>
            /// 腿控
            /// </summary>
            public string ut_name { get; set; }
        }

        public class DataItem
        {
            /// <summary>
            ///
            /// </summary>
            public string uid { get; set; }

            /// <summary>
            ///
            /// </summary>
            public int ctime { get; set; }

            /// <summary>
            /// 这只尼禄超有气质，唔姆
            /// </summary>
            public string content { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string id { get; set; }

            /// <summary>
            ///
            /// </summary>
            public int like_count { get; set; }

            /// <summary>
            ///
            /// </summary>
            public List<CommentsItem> comments { get; set; }

            /// <summary>
            ///
            /// </summary>
            public int comments_count { get; set; }

            /// <summary>
            ///
            /// </summary>
            public List<string> at_users { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string user_liked { get; set; }

            /// <summary>
            /// 一只安静的然
            /// </summary>
            public string uname { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string avatar { get; set; }

            /// <summary>
            ///
            /// </summary>
            public List<UtagsItem> utags { get; set; }
        }

        public class Root
        {
            /// <summary>
            ///
            /// </summary>
            public int status { get; set; }

            /// <summary>
            /// 成功
            /// </summary>
            public string info { get; set; }

            /// <summary>
            ///
            /// </summary>
            public List<DataItem> data { get; set; }
        }
    }
}